package com.xxh.securitydemo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxh.securitydemo.model.UmsAdmin;
import com.xxh.securitydemo.model.UmsMenu;
import com.xxh.securitydemo.service.AdminService;
import com.xxh.securitydemo.utils.JWTUtils;
import com.xxh.securitydemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.util.annotation.Nullable;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class UmsAdminController {

    @Autowired
    JWTUtils jwtUtils;

    @Autowired
    AdminService adminService;

    @RequestMapping(value = "info", method = RequestMethod.GET)
    public Result getUserInfo(String token) {
        String name = jwtUtils.getUserNameFromToken(token);
        UmsAdmin admin = adminService.getAdminByUsername(name);
        //获取用户可以访问的菜单
        List<UmsMenu> menus = adminService.getMenusByUid(admin.getId());
        return Result.ok().put("name", admin.getUsername()).put("avatar", admin.getIcon()).put("mens", menus);
    }

    @GetMapping("/list")
    public Result listAdmin(@RequestParam(value = "page" ,defaultValue = "1") @Nullable String page,
                            @RequestParam(value = "size",defaultValue = "5") @Nullable String size){

        IPage<UmsAdmin> adminList = adminService.page(new Page(Long.valueOf(page), Long.valueOf(size)));
        return Result.ok().put("adminData",adminList);
    }
}
